"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
This is the main launcher for the trie dictionary program
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""


from triedict import TrieDictionary
from triedict import SuffixTrie
import re



# Load Dictionary from file
def load_dictionary():

    # The dictionary text file
    filepath = "testDictionary.txt" # testDictionary.txt or OSPD4.txt

    # Regex to find the word on each line of the file
    word_regex = "^[A-Z]+(?=\\s?)"

    # Open file for reading
    with open(filepath, "r") as dictfile:
        line = dictfile.readline()
        while len(line) > 0:
            # get word and meaning from line
            match = re.search(word_regex, line)
            if match:
                word = match.group()
                meaning = line[len(word) + 1:]
                #trie_dictionary.add_word_and_meaning(word, meaning)
                suffix_trie.add_word(word)
            else:
                print ("No word found on line:", line)

            # read next line
            line = dictfile.readline()




# Find words
def find_prefix():
    print ('\nFinds all the words that start with a prefix.')
    prefix = input("\nEnter a prefix:")

    if prefix == "":
        print ("\n!!That is not a valid value.!!")
        return
    else:
        output = trie_dictionary.get_words_with_prefix(prefix)
        if output is None:
            print ("\n!!There are no words that start with \"" + prefix + "\"!!")
        else:
            print ('\n')
            print (output)
            print ('\n' + str(len(output)), 'words start with \"' + prefix + '\"')


# Find Meaning
def find_meaning():
    print ('\nFind the meaning of a word.')
    word = input("\nEnter a word:")

    if word == "":
        print ('\n!!That is not a valid word!!')
        return
    else:
        meaning = trie_dictionary.get_meaning(word)
        if meaning is None:
            print ("\n!!The word was not found!!")
        else:
            print ('\n' + word.upper(), '=', meaning)



# Find words
def find_suffix():
    print ('\nFinds all the words that end with a suffix.')
    suffix = input("\nEnter a suffix:")

    if suffix == "":
        print ("\n!!That is not a valid value.!!")
        return
    else:
        output = suffix_trie.get_words_with_suffix(suffix)
        if output is None:
            print ("\n!!There are no words that end with \"" + suffix + "\"!!")
        else:
            print ('\n')
            print (output)
            print ('\n' + str(len(output)), 'words end with \"' + suffix + '\"')



"""""""""""""""
Program Entry
"""""""""""""""

# Information
print("This program lets you:" \
    "\n\t-\tfind the [M]eaning of a word" \
    "\n\t-\tfind words that start with a [P]refix" \
    "\n\t-\tfind words that end with a [S]uffix")

# Load dictionary
trie_dictionary = TrieDictionary()
suffix_trie = SuffixTrie()
print ("\nLoading dictionary...")
load_dictionary()
print ("Finished loading dictionary")


# Ask for input until user quits
while True:
    print ('\n' + '='*100 + '\n')
    print ('WHAT DO YOU WANT TO DO? [To exit, leave it blank and press Enter]')

    # Get user input
    response = raw_input("Enter P for Prefix, M for Meaning or S for Suffix:")
    response = response.upper()

    if response == "P":
        find_prefix()
    elif response == "M":
        find_meaning()
    elif response == "S":
        find_suffix()
    elif response == "":
        print ('\nProgram exited by user')
        exit()
    else:
        print ("\nYour input was not recognized. Try again.")



